package com.greateffect.littlebud.lib.base;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.zcs.autolayout.utils.AutoUtils;

import com.greateffect.littlebud.lib.R;
import com.greateffect.littlebud.lib.listener.OnCommonListener;

/**
 * Created by ZengCS on 2019/1/29.
 * function：popupwindow 的父类
 */
public abstract class BasePopupWindow<T> extends PopupWindow {
    protected Activity mActivity = null;
    protected T mData;
    protected int mWidth;
    protected int mHeight;

    protected OnCommonListener callback;

    public BasePopupWindow(Activity activity, T data) {
        this(activity,data,0);
    }

    public BasePopupWindow(Activity activity, T data, int width) {
        this(activity,data,width,0);
    }
    public BasePopupWindow(Activity activity, T data, int width, int height) {
        super(activity);
        this.mData = data;
        this.mWidth = width;
        this.mHeight = height;
        init(activity);
    }

    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    //默认的弹出动画，若修改需要在子类中重新改方法
    public int getAnimationStyle(){
        return R.style.top_to_bottom_popwindow;
    }

    private void init(Activity activity){
        this.mActivity = activity;

        View view = LayoutInflater.from(activity).inflate(getLayoutId(),null);
        initView(view);
        AutoUtils.auto(view);
        setContentView(view);
        //TODO 兼容5.0系统 5.0必须设置宽度和高度，否则popupwindow弹不出来
        setWidth(mWidth <= 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : mWidth);
        setHeight(mHeight <= 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : mHeight);
        setAlpha(0.7f);
        // 设置SelectPicPopupWindow弹出窗体可点击
        setFocusable(true);
        setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        setBackgroundDrawable(dw);
        setAnimationStyle(getAnimationStyle());
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1.0f);
                onExtraDismissListener();
            }
        });

    }

    /**
     * 弹窗关闭后的事件
     */
    public void onExtraDismissListener(){

    }

    /**
     * 设置背景窗口的灰度
     * @param alpha
     */
    private void setAlpha(float alpha){
        if(mActivity != null){
            WindowManager.LayoutParams params=mActivity.getWindow().getAttributes();
            params.alpha=alpha;
            mActivity.getWindow().setAttributes(params);
        }
    }

    public void show(View view, int offX, int offY, boolean isNeedAlpha){
        if(isNeedAlpha){
            setAlpha(0.7f);
        }else {
            setAlpha(1.0f);
        }
        showAsDropDown(view,offX,offY);
    }

    protected void showToast(String msg){
        if(!TextUtils.isEmpty(msg)){
            Toast.makeText(mActivity,msg, Toast.LENGTH_SHORT).show();
        }
    }

    public <T> void setCallback(OnCommonListener<T> callback) {
        this.callback = callback;
    }
}
